import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class Factorizator {
	private int counter = 0;

	private int[] primes;

	public Factorizator(int[] primes) {
		this.primes = primes;
	}

	private int[] factorize(int number) {
		counter++;

		List<Integer> fact = new ArrayList<>();

		int primeIndex = 0;
		while (number > 1) {
			if (number > 1 && number % primes[primeIndex] == 0) {
				number /= primes[primeIndex];
				fact.add(primes[primeIndex]);
			} else {
				primeIndex++;
			}
		}

		int[] result;
		result = DKUtil.makeIntArray(fact);

		return result;
	}

	public int[] expensiveFactorize(int number) {
		counter++;

		List<Integer> fact = new ArrayList<>();

		int prime = 2;
		while (number > 1) {
			if (number > 1 && number % prime == 0) {
				number /= prime;
				fact.add(prime);
			} else {
				prime++;
				while (!isPrime(prime)) {
					prime++;
				}
			}
		}

		int[] result;
		result = DKUtil.makeIntArray(fact);

		return result;
	}

	private boolean isPrime(int number) {
		for (int divisor = 2; divisor < number; divisor++) {
			if (number % divisor == 0) {
				return false;
			}
		}
		return true;
	}

	public SortedMap<Integer, int[]> factorizeAllLEQThan(int maxNumber) {
		counter++;

		SortedMap<Integer, int[]> factMap = new TreeMap<Integer, int[]>();

		for (int number = 2; number <= maxNumber; number++) {
			factMap.put(number, factorize(number));
		}

		return factMap;
	}

	public void doLongJob(int maxNumber) {
		factorizeAllLEQThan(maxNumber);
	}

	public int getCount() {
		return counter;
	}

	public int[] compute(Integer number) throws InterruptedException {
		return factorize(number);
	}
}
